package top.zhdev.community.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.util.HtmlUtils;
import top.zhdev.community.entity.Message;
import top.zhdev.community.mapper.MessageMapper;
import top.zhdev.community.service.MessageService;
import top.zhdev.community.utils.SensitiveFilter;

import java.util.List;

/**
 * 私信业务处理
 */
@Service
public class MessageServiceImpl implements MessageService {

    private MessageMapper messageMapper;
    private SensitiveFilter sensitiveFilter;

    @Autowired
    public void setMessageMapper(MessageMapper messageMapper){
        this.messageMapper = messageMapper;
    }

    @Autowired
    public void setSensitiveFilter(SensitiveFilter sensitiveFilter){
        this.sensitiveFilter = sensitiveFilter;
    }

    /**
     * 查询指定用户的私信数量
     * @param userId
     * @return
     */
    @Override
    public int getConversationCount(int userId) {
       return messageMapper.getConversationCount(userId);
    }

    /**
     * 查询指定用户的私信
     * @param userId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Message> getConversations(Integer userId, int offset, int limit) {
        return messageMapper.getConversations(userId,offset,limit);
    }

    /**
     * 获取指定对话的私信数量
     * @param conversationId
     * @return
     */
    @Override
    public int getLetterCount(String conversationId) {
        return messageMapper.getLetterCount(conversationId);
    }

    /**
     * 获取用户未读消息数
     * @param userId
     * @param conversationId
     * @return
     */
    @Override
    public int getLetterUnreadCount(Integer userId, String conversationId) {
       return messageMapper.getLetterUnreadCount(userId,conversationId);
    }

    /**
     * 查询评论类通知
     * @param userId
     * @param topic
     * @return
     */
    @Override
    public Message getLatesNotice(Integer userId, String topic) {
        return messageMapper.getLatesNotice(userId,topic);
    }

    /**
     * 获取系统消息数量
     * @param userId
     * @param topic
     * @return
     */
    @Override
    public int getNoticeCount(Integer userId, String topic) {
        return messageMapper.getNoticeCount(userId,topic);
    }

    /**
     * 获取系统未读消息数量
     * @param userId
     * @param topic
     * @return
     */
    @Override
    public int getNoticeUnreadCount(Integer userId, String topic) {
        return messageMapper.getNoticeUnreadCount(userId,topic);
    }

    /**
     * 创建一个消息
     * @param message
     * @return
     */
    @Override
    public int addMessage(Message message) {
        String content = HtmlUtils.htmlEscape(message.getContent());
        // 敏感词过滤
        message.setContent(sensitiveFilter.filter(content));
        return messageMapper.saveMessage(message);
    }

    /**
     * 获取私信列表
     * @param conversationId
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Message> getLetters(String conversationId, int offset, int limit) {
        return messageMapper.getLetters(conversationId,offset,limit);
    }

    /**
     * 更改未读消息状态为已读
     * @param ids
     * @return
     */
    @Override
    public int readMessage(List<Integer> ids) {
        return messageMapper.updateStatus(ids,1);
    }

    /**
     * 获取系统通知列表
     * @param userId
     * @param topic
     * @param offset
     * @param limit
     * @return
     */
    @Override
    public List<Message> getNotices(Integer userId, String topic, int offset, int limit) {
        return messageMapper.getNotices(userId, topic,offset,limit);
    }
}
